package ca.etsmtl.log430.lab3.gestion.list;

import java.util.HashMap;

import ca.etsmtl.log430.lab3.gestion.Course;
import ca.etsmtl.log430.lab3.gestion.CourseType;

public class CourseList extends List {
	
	/**
	 * @uml.property  name="courseIdMap"
	 * @uml.associationEnd  qualifier="getCourseID:java.lang.String ca.etsmtl.log430.lab3.gestion.Course"
	 */
	HashMap<String,Course> courseIdMap = new HashMap<String,Course>();

	public CourseList() {

		super();
		courseIdMap = new HashMap<String,Course>();

	} // Constructor

	/**
	 * @param course
	 *            New course to be added to the list. All the issues of casting
	 *            are taken care of within this class.
	 */
	public void addCourse(Course course) {
		
		appendItemToList(course);
		courseIdMap.put(course.getCourseID(), course);
	} // AddCourse

	/**
	 * @return The course pointed at the current position pointed to by the
	 *         internal list pointer of the internal list. Subsequent calls will
	 *         return the next Course object in the list. A null object is
	 *         returned if list is empty or the end of list has been reached.
	 */
	public Course getNextCourse() {
		return (Course) getItemFromList();
	} // GetNextCourse

	/**
	 * This method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param course
	 * @return A Course instance if found in the list based on specified
	 *         criteria, null otherwise.
	 */
	public boolean findCourse(Course course) {

		Course currentObject;
		boolean done = false;
		boolean result = false;

		goToFrontOfList();

		while (!done) {

			currentObject = getNextCourse();

			if (currentObject == null) {

				done = true;

			} else {
				if (course.getCourseID().compareToIgnoreCase(
						currentObject.getCourseID()) == 0) {

					result = true;

				} // if

			} // if

		} // while

		return (result);

	} // FindCourse

	
	//public CourseList findCoursesB
	

	
	
	/**
	 * This method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param courseID
	 * @param group
	 * @return A Course instance if found in the list based on specified
	 *         criteria, null otherwise.
	 */
	public Course findCourseByIDActivityAndGroup(String courseID, String group, CourseType type) {

		Course currentObject = null;
		boolean done = false;
		boolean found = false;

		goToFrontOfList();

		while (!done) {

			currentObject = getNextCourse();

			if (currentObject == null) {

				done = true;

			} else {

				if ((currentObject.getCourseID().compareToIgnoreCase(courseID) == 0)
						&& (currentObject.getGroup().compareToIgnoreCase(group) == 0)
						&& (currentObject.getType().compareTo(type) == 0)) {

					found = true;
					done = true;

				} // if

			} // if

		} // while

		if (found) {

			return (currentObject);

		} else {

			return (null);

		} // if

	} // findCourseByIDAndGroup

	public Course findcoursByID(String courseId) {
		return courseIdMap.get(courseId);
	}

} // CourseList
